home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / amem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-14  |  1.2 KB  |  60 lines

  1.  
  2. /* Copyright (C) 1988, 1989 Herve' Touati, Aquarius Project, UC Berkeley */
  3.  
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/time.h>
  7. #include <sys/resource.h>
  8.  
  9. extern caddr_t brk();
  10. extern caddr_t sbrk();
  11. extern getrlimit();
  12. extern setrlimit();
  13.  
  14. int test_limit(limit)
  15. int limit;
  16. {
  17.    static caddr_t bottom;
  18.  
  19.    if (bottom == NULL)        /* first call to test_limit */
  20.      bottom = sbrk(0);
  21.   return (brk(bottom + limit) == (caddr_t)(-1)) ? 0 : 1;
  22. }
  23.  
  24. int binary_search(lower, upper, precision, predicate)
  25. int lower, upper, precision, (*predicate)();
  26. {
  27.    int c;
  28.  
  29.    for(;;) {
  30.       if ((upper - lower) < precision)
  31.     return lower;
  32.       c = (lower + upper) / 2;
  33.       if ((*predicate)(c)) 
  34.     lower = c;
  35.       else
  36.     upper = c;
  37.    }
  38. }
  39.      
  40. main()
  41. {
  42.    int lower_limit, upper_limit, precision, exact_limit;
  43.    struct rlimit rl;
  44.  
  45.    if (getrlimit(RLIMIT_DATA, &rl)) return -1;
  46.    rl.rlim_cur = rl.rlim_max;
  47.    if (setrlimit(RLIMIT_DATA, &rl)) return -1;
  48.    printf("%d kbytes system limit\n", rl.rlim_cur / 1000);
  49.  
  50.    lower_limit = getpagesize();
  51.    precision = lower_limit / 2;
  52.    upper_limit = rl.rlim_cur;
  53.    exact_limit = binary_search(lower_limit,
  54.                    upper_limit,
  55.                    precision,
  56.                    test_limit);
  57.    printf("%d kbytes available\n", exact_limit / 1000);
  58. }
  59.  
  60.